//
//  XJWalletBankView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/23.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJWalletBankView.h"
@interface XJWalletBankView()

@property(nonatomic, strong) UIView *bgView;

//没有数据的时候的界面
@property(nonatomic, strong) UIImageView *noDataIv;
@property(nonatomic, strong) UILabel *addBankTipsLb; //添加银行卡



//=========================
//有数据的时候的界面
@property(nonatomic, strong) UIImageView *iv;
@property(nonatomic, strong) UILabel *bankLb; //中国银行 亳州支行
@property(nonatomic, strong) UILabel *cardNumLb; //银行卡号lb xxxxxxx874  张三
@property(nonatomic, strong) UIButton *editButton; //编辑按钮

@end

@implementation XJWalletBankView

#pragma mark - 一 lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}
//没有数据的时候的界面
//=========================
- (UIImageView *)noDataIv {
    if (!_noDataIv) {
        _noDataIv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bj_account_card"]];
        _noDataIv.backgroundColor = [UIColor lightGrayColor];
        _noDataIv.layer.masksToBounds = YES;
        _noDataIv.layer.cornerRadius = 10.0;
        _noDataIv.userInteractionEnabled = YES;
        
        //添加手势
        UITapGestureRecognizer *noDataTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(noDataTapGesAction:)];
        [_noDataIv addGestureRecognizer:noDataTapGes];
    }
    return _noDataIv;
}

- (UILabel *)addBankTipsLb {
    if (!_addBankTipsLb) {
        _addBankTipsLb = [UILabel new];
//        _addBankTipsLb.textAlignment = NSTextAlignmentCenter;
        _addBankTipsLb.text = @"点击添加银行卡";
    }
    return _addBankTipsLb;
}




- (UIImageView *)iv {
    if (!_iv) {
        _iv = [[UIImageView alloc] init];
        _iv.image = [UIImage imageNamed:@"bj_account_card"];
        _iv.backgroundColor = XJCOLOR_RGB(223, 78, 81); //红
        _iv.layer.masksToBounds = YES;
        _iv.layer.cornerRadius = 10.0;
    }
    return _iv;
}
- (UILabel *)bankLb {
    if (!_bankLb) {
        _bankLb = [[UILabel alloc] init];
        _bankLb.textColor = [UIColor whiteColor];
        _bankLb.text = @"中国银行 亳州支行";
    }
    return _bankLb;
}
- (UILabel *)cardNumLb {
    if (!_cardNumLb) {
        _cardNumLb = [UILabel new];
        _cardNumLb.text = @"xxxxxxx897 张三";
        _cardNumLb.textColor = [UIColor whiteColor];
    }
    return _cardNumLb;
}

- (UIButton *)editButton {
    if (!_editButton) {
        _editButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_editButton setTitle:@"编辑" forState:(UIControlStateNormal)];
        [_editButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _editButton.titleLabel.font = XJFONTSET_R(14.0);
        
        [_editButton setImage:[UIImage imageNamed:@"address_edit"] forState:(UIControlStateNormal)];
        [_editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _editButton;
}


#pragma mark - 二 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView {
    [self addSubview:self.bgView];
    
    
    //有数据的界面
    [self.bgView addSubview:self.iv];
    [self.bgView addSubview:self.bankLb];
    [self.bgView addSubview:self.cardNumLb];
    [self.bgView addSubview:self.editButton];
    
    
    //没有数据的界面:后添加,把上边的盖
    {
        [self.bgView addSubview:self.noDataIv];
        [self.noDataIv addSubview:self.addBankTipsLb];
    }
    
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    CGFloat k10 = 10.0;
    CGFloat k60 = ADAPTHeight(60);
    
    
    
    //没有数据的界面
    {
        [self.noDataIv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView.mas_left).offset(k10);
            make.top.bottom.equalTo(self);
            make.right.equalTo(self.bgView.mas_right).offset(-k10);
        }];
        
        [self.addBankTipsLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.noDataIv.mas_left).offset(k60);
            make.centerY.equalTo(self.noDataIv.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(200, 30));
        }];
    }
    
    //数据的界面
    [self.iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(k10);
        make.top.bottom.equalTo(self);
        make.right.equalTo(self.bgView.mas_right).offset(-k10);
    }];
    
    
    [self.bankLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iv.mas_left).offset(k60);
        make.top.equalTo(self.iv.mas_top).offset(k10);
        make.height.mas_equalTo(30);
        make.right.equalTo(self.iv.mas_right).offset(-10);
    }];
    
    [self.cardNumLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iv.mas_left).offset(k60);
        make.right.equalTo(self.editButton.mas_right).offset(5);
        make.bottom.equalTo(self.iv.mas_bottom).offset(-k10);
        make.height.mas_equalTo(30);
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.iv.mas_right).offset(-k10);
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.bottom.equalTo(self.iv.mas_bottom).offset(-k10);
    }];
}

#pragma mark - 四 点击事件
- (void)editButtonAction:(UIButton *)button {
//    XJLog(@"点击编辑银行卡");
    if (self.editButtonBlock) {
        self.editButtonBlock();
    }
}

//没有数据的图片的点击手势
- (void)noDataTapGesAction:(UITapGestureRecognizer *)tap {
    if (self.noDataIvClickBlock) {
        self.noDataIvClickBlock();
    }
}


#pragma mark - 四 赋值
- (void)setBankModel:(XJWalletTiXianBankModel *)bankModel {
    
    //判断model是否为空
    if ([NSString isEmptyString:bankModel.bank_name] || [NSString isEmptyString:bankModel.card_number]) {
        XJLog(@"认定为model为空不做处理");
        
        self.noDataIv.hidden = NO;
        return;
    }
    
    
    
    //没数的model隐藏
    self.noDataIv.hidden = YES;
    
    //赋值
    self.bankLb.text = [NSString stringWithFormat:@"%@ %@",bankModel.bank_name,bankModel.bank_branch];
    
    
    //银行卡只显示最后三位数
    CGFloat totalCnt = bankModel.card_number.length;
    NSRange range = NSMakeRange(totalCnt - 3, 3);
    NSString *tempNum = bankModel.card_number;
    NSString *lastThreeNum = [tempNum substringWithRange:range];
    NSMutableString *xxxStr = [NSMutableString string];
    for (int i=0; i<totalCnt-3; i++) {
        [xxxStr appendFormat:@"*"];
    }
    
    [xxxStr appendFormat:@"%@",lastThreeNum];
    
    XJLog(@"%@",xxxStr);
    
    //拼接用户名
    [xxxStr appendFormat:@" %@",bankModel.card_name];
    
    self.cardNumLb.text = xxxStr;
}
@end
